import { Message } from "element-ui";
import { save } from "@/api/second.js";
import { queryTreeData } from "@/api/common.js";
export default {
  name: "maintenanceRecord",
  data() {
    return {
      form: {
        energyPlanTarget: "促进新能源消纳",
        planName: "",
        pConstraint: 5000,
        energyConstraint: 10000,
        countConstraint: 3,
        ghdxbdz: "",
        feederId: "",
      },
      dialogVisible: false,
      percentage: 20,
      rules: {
        planName: [
          { required: true, message: "请输入规划方案名称", trigger: "blur" },
        ],
        pConstraint: [
          { required: true, message: "请输入功率约束", trigger: "blur" },
        ],
        energyConstraint: [
          { required: true, message: "请输入容量约束", trigger: "blur" },
        ],
        countConstraint: [
          { required: true, message: "请输入数量约束", trigger: "blur" },
        ],
        ghdxbdz: [
          { required: true, message: "请选择变电站", trigger: "change" },
        ],
        feederId: [
          { required: true, message: "请选择规馈线", trigger: "change" },
        ],
      },

      // 变电站下拉列表
      stationOptions: [],
      // 馈线下拉列表
      lineOptions: [],
    };
  },

  mounted() {
    this.dxtInit();
    this.form.planName =
      this.form.energyPlanTarget +
      "" +
      this.dateFormat(new Date().getTime()) +
      " " +
      new Date().toTimeString().split(" ")[0].substr(0, 5) +
      "规划方案";
  },
  created() {
    this.getTreeData();
    this.queryFeeder();
    // this.regulationOrgList()
    // this.stationTypeList()
    // this.adjustRegulationList()
    // this.voltageLevelList()
    // this.getTreeData()
    // this.queryStationTestNumber()
  },
  methods: {
    // 、获取树结构
    getTreeData() {
      queryTreeData().then((res) => {
        if (res.resultCode === 0) {
          this.stationOptions = [];
          res.data.forEach((el) => {
            this.stationOptions.push({
              label: el.substationName,
              value: el.substationId,
            });
          });
        }
      });
    },

    queryFeeder() {
      this.lineOptions = [];
      queryTreeData(this.queryDetailParam).then((res) => {
        if (res.resultCode === 0) {
          for (var i = 0; i < res.data.length; i++) {
            // let feedersList =  res.data[i].feeders;
            for (var x = 0; x < res.data[i].feeders.length; x++) {
              var obj = {};
              obj.label = res.data[i].feeders[x].name;
              obj.value = res.data[i].feeders[x].feederId;
              this.lineOptions.push(obj);
            }
          }
        }
      });
    },

    //保存
    saveData() {
      console.log(this.form);
      let params = JSON.parse(JSON.stringify(this.form));
      params.energyPlanTarget =
        this.form.energyPlanTarget == "促进新能源消纳" ? 1 : 2;
      save(params).then((res) => {
        if (res.resultCode === 0) {
          this.$message({
            message: "添加成功",
            type: "success",
          });
          this.$router.push({ name: "sycngh" });
          // this.dataTree = res.data
          // console.log(res.data);
        } else {
          this.$message.error("添加失败");
        }
      });
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.form.energyPlanTarget = "促进新能源消纳";
      this.form.planName =
        this.form.energyPlanTarget +
        "" +
        this.dateFormat(new Date().getTime()) +
        "规划方案";
    },
    dateFormat(val) {
      let date = new Date(val);
      let year = date.getFullYear();
      let month =
        date.getMonth() + 1 > 9
          ? date.getMonth() + 1
          : "0" + (date.getMonth() + 1);
      let day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();

      return year + "-" + month + "-" + day;
    },

    customColorMethod(percentage) {
      if (percentage < 30) {
        return "#909399";
      } else if (percentage < 70) {
        return "#e6a23c";
      } else {
        return "#67c23a";
      }
    },

    open() {
      let timer = setTimeout(() => {
        //需要定时执行的代码
        this.increase();
        if ((this.percentage = 100)) {
          this.saveData();
          clearTimeout(timer);
        }
      }, 2000);
    },
    increase() {
      this.percentage += 10;
      if (this.percentage > 100) {
        this.percentage = 100;
      }
    },

    onSubmit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.dialogVisible = true;
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.dialogVisible = false;
      this.percentage = 20;
      // this.$router.push({ name: "sycngh" });
    },

    // dxtInit(){
    // 	let gzqytmp='keyidrect=FFCC33_';
    // 	if(this.gzqyid)gzqytmp+=this.gzqyid;
    // 	let faultdescId = this.deviceIdlist.join(':');
    // 	gzqytmp += ';faultdesc=' + faultdescId;
    // 	let bigStr = 'graph=' + this.rowParamsObj.dxt + ';isClient=1;menubarshow=0;appmode=3;isFault=1;100p;100p;isgray=0;' + gzqytmp;
    // 	console.log(bigStr);
    // 	let msg = JSON.stringify({
    // 		fun: 'urlUtilGraph',
    // 		param0: bigStr,
    // 	});
    // 	setTimeout(() => {
    // 		this.$refs.dxt.contentWindow.postMessage(msg, '*');
    // 	}, 2000)
    // },
    dxtInit() {
      // this.dxtUrl=dxtUrl+'?menubarshow=0;toolbarshow=0;graph=' + this.rowParamsObj.dxt;

      this.dxtUrl =
        "http://pdyzz.test.com.cn/graph/Navigator.html?graph=新西169线单线图.sln.pic.g";
    },

    radioChangeValueFn(val) {
      this.form.planName =
        val + "" + this.dateFormat(new Date().getTime()) + "规划方案";
    },

    changeFeederFn(val) {
      this.form.feederId = "";
      let params = {
        substationId: val,
      };
      queryTreeData(params).then((res) => {
        this.lineOptions = [];
        if (res.resultCode === 0) {
          res.data[0].feeders.forEach((el) => {
            this.lineOptions.push({
              label: el.name,
              value: el.feederId,
            });
          });
        }
      });
    },

    // 获取渲染树的数据
    // getTreeData(data) {
    //   queryOfExperimentalresults(data).then((res) => {
    //     this.dataTree = addStationName(res.data);
    //     // console.log(data.stationId+"111111");
    //     this.$nextTick(function () {
    //       if (data == undefined) {
    //         this.$refs.equipmentTree.setCheckedKeys([]);
    //         this.checkList = [];
    //         this.queryParamStationId = "";
    //       } else {
    //         this.$refs.equipmentTree.setCheckedKeys([data.stationId], false);
    //         let list = [data.stationId];
    //         this.queryParamStationId = data.stationId;
    //         console.log(list);
    //         this.checkList = list;

    //         let params = {
    //           regulationOrgIds: [],
    //           stationIds: list,
    //           fileSort: "01",
    //         };
    //         this.queryDetailParam = params;
    //         params.stationIds.forEach((e, i) => {
    //           // console.log(params.regulationOrgIds.includes(getAllParentNodes(this.dataTree,e)[0].id))
    //           if (
    //             params.regulationOrgIds.includes(
    //               getAllParentNodes(this.dataTree, e)[0].id
    //             )
    //           ) {
    //             // 找到这个元素删掉他
    //             this.queryDetailParam.stationIds =
    //               this.queryDetailParam.stationIds.filter((d) => d !== e);
    //           }
    //         });
    //         this.$nextTick(() => {
    //           this.$refs.windPowerTable.getDataInit();
    //         });
    //       }
    //     });
    //   });
    // },
  },
};
